<?php
class TurnoverAction extends Action{
	public function _initialize(){

		$action = array(

			'users'=>array('index', 'add','search','lists', 'edit', 'del'),

			'anonymous'=>array('')

		);

		B('Authenticate', $action);

	}
	public function income_add(){
		$this->display();
	}
	public function turnover_add(){
		$dep=D("Turnover")->getDepartment2();
		
		$tdate=strtotime(date("Y-m-d"));
		$this->assign("dep",$dep);
		$this->assign("tdate",$tdate);
		$this->display();
	}
	public function turnover_add2(){
		$dep=$_POST['dep'];
		$tdate=$_POST['tdate'];
		$money=$_POST['money'];
		$desc=$_POST['desc'];
		$d1=strtotime($tdate);
		$atime=time();
		$data=[
		'money_sum'=>$money,
		'dep'=>$dep,
		'desc'=>$desc,
		'add_time'=>$atime,
		'total_date'=>$d1,
		'type'=>"to",
		'status'=>1
		];
		M('turnover_total')->add($data);
		$this->redirect("table_list",array("st"=>"turnover"));
	}
	public function expend_add(){
		$types=D('Turnover')->getTypeByAction("expend");
		foreach($types as $key=>$vo){
			if($vo['parent_id']==0){
				$type_list[$vo['tid']]=$vo;
			}else{
				$type_list[$vo['parent_id']]['power'][$vo['tid']]=$vo;
			}
		}
		//dump($type_list);
		$expend_date=strtotime("now");
		$department_list = D('User')->getAllsublist();
		$this->assign('expend_date', $expend_date);
		$this->assign('dep', $department_list);
		$this->assign('types',$type_list);
		$this->display();
	}
	
	public function expend_add2(){
		$types=D('Turnover')->getTypeByAction("expend");
		$post_list=array();
		$dep=$_POST['dep'];
		$desc=$_POST['desc'];
		$total_date=strtotime($_POST['tdate']);

$money_sum=0.00;
		foreach($types as $key=>$vo){
			if($vo['parent_id'] !=0){
				if(!empty($_POST[$vo['tid']]) || $_POST[$vo['tid']] !=''){
					$post_list[$vo['tid']]=(double)$_POST[$vo['tid']];
					$money_sum+=(double)$_POST[$vo['tid']];
				}
				
			}
		}
		$add_time=strtotime("now");
		$total_data['money_sum']=$money_sum;
		$total_data['desc']=$desc;
		$total_data['add_time']=$add_time;
		$total_data['total_date']=$total_date;
		$total_data['type']="expend";
		$total_data['dep']=$dep;
		D('Turnover')->addTotal($total_data);
		$total_id=D('Turnover')->getTotalByTime($add_time);
		foreach($post_list as $key=>$p){
			$data['tid']=$key;
			$data['money']=$p;
			$data['total_id']=$total_id;
			D('Turnover')->addMoney($data);
		}
		
		$this->redirect("expend",array('total_id'=>$total_id));
	}
	public function expend($total_id){
		$list=D('Turnover')->getExpend($total_id);

		$this->assign('list',$list);
		$this->display();
	}
	public function expend_update2(){
		$types=D('Turnover')->getTypeByAction("expend");
		$post_list=array();
		$total_id=$_POST['total_id'];
		$dep=$_POST['dep'];
		$desc=$_POST['desc'];
		$expend_date=$_POST['expend_date'];
		$total_date=strtotime($expend_date);
		
$money_sum=0.00;
		foreach($types as $key=>$vo){
			if($vo['parent_id'] !=0){
				if(!empty($_POST[$vo['tid']]) || $_POST[$vo['tid']] !=''){
					$post_list[$vo['tid']]=(double)$_POST[$vo['tid']];
					$money_sum+=(double)$_POST[$vo['tid']];
				}
				
			}
		}
		$whtotal=array(
			'total_id'=>$total_id		
		);
		$add_time=strtotime("now");
		$total_data['money_sum']=$money_sum;
		$total_data['desc']=$desc;
		$total_data['add_time']=$add_time;
		$total_data['total_date']=$total_date;
		$total_data['type']="expend";
		$total_data['dep']=$dep;
		M('turnover_total')->where($whtotal)->save($total_data);
		
		foreach($post_list as $key=>$p){
			$whm=array(
			'tid'=>$key,
			'total_id'=>$total_id
			);
			$data['money']=$p;
			$mn=M('turnover_money')->where($whm)->find();
			if(!empty($mn)){
				M('turnover_money')->where($whm)->save($data);
			}else{
				$data2=[
						'tid'=>$key,
						'total_id'=>$total_id,
						'money'=>$p		
				];
				M('turnover_money')->add($data2);
			}
			

		}
		
		$this->redirect("expend",array('total_id'=>$total_id));
	}
	public function expend_update($total_id){
		$list=D('Turnover')->getExpend($total_id);

		$department_list = D('User')->getAllsublist();
		$this->assign('dep', $department_list);
		$this->assign('list',$list);
		$this->display();
	}
	public function turnover_update($total_id){
		$list=D('Turnover')->getExpend($total_id);

		$department_list = D('Turnover')->getDepartment2();
		$this->assign('dep', $department_list);
		$this->assign('list',$list);
		$this->display();
	}
	public function table_list($st="turnover",$str=0){
		$path="turnover";
		if($st=="expend"){
			$list=D('Turnover')->getTotalByType("expend");
			$path="expend";
		}
		elseif($st=="income"){
			$list=D('Turnover')->getTotalByType("income");	
			$path="income";
		}
		elseif($st=="turnover"){
			$list=D('Turnover')->getTotalByType("to");	
		}
		
		if(count($list)>0){
				foreach($income as $key=>$vo){
					$department=D('Turnover')->getDepartment($vo['dep']);
					$list[$key]['department']=$department;
				}
		}
		$tdate=time();
		$this->assign('list',$list);
		$this->assign('path',$path);
		$this->assign('str',$str);
		$this->assign('tdate',$tdate);
		$this->display();
	}
	
	public function search_list(){
		$tdate=$_POST['tdate'];
		$action=$_POST['action'];
		$form=$_POST['form'];
		$d1=strtotime($tdate);
		if($action=="turnover"){$action="to";}
		if($form=="day"){
			//按日期查询
			$d2=$d1+86400;
			$list=D("Turnover")->getTotalByDate($d1,$d2,$action);
		}
		elseif($form=="month"){
			$days=date("t",$d1);
			$m1=$tdate."-01";
			$m2=$tdate."-".$days;
			$d1=strtotime($m1);
			$d2=strtotime($m2)+86400;
			$list=D("Turnover")->getTotalByDate($d1,$d2,$action);
		}
		if($action=="to"){$action="turnover";}
		$this->assign('list',$list);
		$this->assign('path',$action);
		$this->assign('tdate',$d1);
		$this->display("table_list");
	}
	public function table(){
		$tdate=$_POST['tdate'];
		$form=$_POST['form'];
		//如果为空，默认取出本月图表
		if(empty($form)){
			$month=date("Y-m");
			$d1=strtotime($month);
			$days=date("t",$d1);
			$last=$month."-".$days;
			$d2=strtotime($last)+86399;
			$list=D('Turnover')->getTableByDate($d1,$d2);
		}
		if($form=="day"){
			$d1=strtotime($tdate);
			$d2=$d1+86399;
			$list=D('Turnover')->getTableByDate($d1,$d2);
		}elseif($form=="month"){
			$d1=strtotime($tdate);
			$days=date("t",$d1);
			$last=$tdate."-".$days;
			$d2=strtotime($last)+86399;
			$list=D('Turnover')->getTableByDate($d1,$d2);
			
		}elseif($form=="year"){
			$year1=$tdate."-01-01";
			$year2=((int)$tdate+1)."-01-01";
			$d1=strtotime($year1);
			$d2=strtotime($year2)-1;
			$list=D('Turnover')->getTableByDate($d1,$d2);
		}
		
	
		$this->assign("d1",$d1);
		$this->assign("d2",$d2);
		$this->assign("list",$list);
		$this->display();
	}
	public function total_delete($total_id){
		$data=[
			'status'=>0
		];
		M('turnover_total')->where(array("total_id"=>$total_id))->save($data);
		$this->redirect("table_list",array("st"=>"turnover"));
	}
	public function total_export(){
		$type=$_POST['type'];
		$tdate=$_POST['tdate'];
		$radio=$_POST['radio'];
		if($type=='to'){$tname="营业额";}
		elseif($type=='expend'){$tname="支出";}
		if($radio==1){
			$d1=strtotime($tdate);
			$d2=strtotime($tdate)+86399;
			$ename=date("Y年m月d日",$d1);
		}elseif($radio==2){
			$darray=explode("-",$tdate);
			$day1=$darray['0']."-".$darray['1']."-01";
			$days=date("t",$tdate);
			$day2=$darray['0']."-".$darray['1']."-".$days;
			$d1=strtotime($day1);
			$d2=strtotime($day2)-1;
			$ename=date("Y年m月",$d1);
		}elseif($radio==3){
			$darray=explode("-",$tdate);
			$day1=$darray['0']."-01-01";
			$day2=($darray['0']+1)."-01-01";
			$d1=strtotime($day1);
			$d2=strtotime($day2)-1;
			$ename=date("Y年",$d1);
		}
		
		$list=M('turnover_total')->where("type='{$type}' AND status=1 AND total_date>={$d1} AND total_date<{$d2}")->order('total_date')->select();
if($list){
		header("Content-Type: text/html; charset=utf-8");
		header("Content-type:application/vnd.ms-execl");
		header("Content-Disposition:filename=".$tname.$ename.".xls");
		//字段
		$letterArr=explode(',',strtoupper('a,b'));
		$arr=array(
			array('en'=>'total_date','cn'=>'日期'),
			array('en'=>'dep','cn'=>'部门'),
			array('en'=>'money_sum','cn'=>'金额'),
			array('en'=>'desc','cn'=>'备注')
		);
		$i=0;
		$fieldCount=count($arr);
		$s=0;
		//thead
		foreach ($arr as $f){
			if ($s<$fieldCount-1){
				echo iconv('utf-8','gbk',$f['cn'])."\t";
			}else {
				echo iconv('utf-8','gbk',$f['cn'])."\n";
			}
			$s++;
		}
		//tbody
		foreach($list as $k=>$v){
			$depname=D('Turnover')->getDepartment($v['dep']);
			echo date("Y-m-d H:i",$v['total_date'])."\t";
			echo iconv('utf-8','gbk',$depname['name'])."\t";
			echo $v['money_sum']."\t";
			echo iconv('utf-8','gbk',$v['desc'])."\n";
		}
		

}else{
	$str="该日期没有数据，无法导出！";
	$this->redirect("table_list",array("st"=>"turnover","str"=>$str));
}
exit;	
	
	
}
	
	
	
}
?>